#include "http_sever.h"

#include <iostream>
#include <memory>
using namespace std;

bool OnXmlService(std::string url, std::string body, mg_connection *conn, OnRspCallback rsp_callback)
{
	std::cout << "Xml Service" << std::endl;
	std::cout << "url: " << url << std::endl;
	std::cout << "body: " << body << std::endl;

	auto index = body.find("<ps:mobile>");
	if (index == body.npos)
	{
		return false;
	}

	std::cout << "mobile = " << body.substr(index + 11, 20) << '\n';

	std::string rsp = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\" http : // schemas.xmlsoap.org/soap/envelope/\">"
					  "<SOAP-ENV:Header/><SOAP-ENV:Body><submitSmsResponse xmlns=\"http://www.example.org/ps-receive\">"
					  "<ssid>1540009140928450941</ssid><status>0</status></submitSmsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";

	rsp_callback(conn, rsp);

	return true;
}

/*
bool handle_fun2(std::string url, std::string body, mg_connection *c, OnRspCallback rsp_callback)
{
	// do sth
	std::cout << "handle fun2" << std::endl;
	std::cout << "url: " << url << std::endl;
	std::cout << "body: " << body << std::endl;

	rsp_callback(c, "rsp2");

	return true;
}
*/

int main()
{
	string port = "20002";
	auto http_server = std::shared_ptr<CHttpServer>(new CHttpServer);

	http_server->Init(port);

	http_server->RegisterHandler("/service/xml", OnXmlService);

	// http_server->RegisterHandler("/api/fun2", handle_fun2);
	http_server->Start();

	return 0;
}
